package es.andresimon.portfolio.domain;

import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import es.andresimon.portfolio.domain.ProjectPicture;

@Component
@Configurable
public class ProjectPictureDataOnDemand {

	private Random rnd = new java.security.SecureRandom();

	private List<ProjectPicture> data;

	public ProjectPicture getNewTransientProjectPicture(int index) {
        es.andresimon.portfolio.domain.ProjectPicture obj = new es.andresimon.portfolio.domain.ProjectPicture();
        obj.setPictureId(new Integer(index).longValue());
        obj.setProjectId(new Integer(index).longValue());
        obj.setWeight(new Integer(index));
        return obj;
    }

	public ProjectPicture getSpecificProjectPicture(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        ProjectPicture obj = data.get(index);
        return ProjectPicture.findProjectPicture(obj.getId());
    }

	public ProjectPicture getRandomProjectPicture() {
        init();
        ProjectPicture obj = data.get(rnd.nextInt(data.size()));
        return ProjectPicture.findProjectPicture(obj.getId());
    }

	public boolean modifyProjectPicture(ProjectPicture obj) {
        return false;
    }

	public void init() {
        data = es.andresimon.portfolio.domain.ProjectPicture.findProjectPictureEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'ProjectPicture' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new java.util.ArrayList<es.andresimon.portfolio.domain.ProjectPicture>();
        for (int i = 0; i < 10; i++) {
            es.andresimon.portfolio.domain.ProjectPicture obj = getNewTransientProjectPicture(i);
            obj.persist();
            obj.flush();
            data.add(obj);
        }
    }
}
